package cn.csc.module.pay.dal.mysql.mqrecord;

import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.mybatis.core.mapper.BaseMapperX;
import cn.csc.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.csc.framework.mybatis.core.query.QueryWrapperX;
import cn.csc.module.pay.controller.app.mqrecord.vo.AppPayMqRecordPageReqVO;
import cn.csc.module.pay.dal.dataobject.mqrecord.PayMqRecordDO;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.util.StringUtils;

import java.math.BigInteger;

/**
 * mq消息消费流水 Mapper
 *
 * @author liuchuang
 */
@Mapper
public interface PayMqRecordMapper extends BaseMapperX<PayMqRecordDO> {

    default PageResult<PayMqRecordDO> selectPage(AppPayMqRecordPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<PayMqRecordDO>()
                .eqIfPresent(PayMqRecordDO::getBusiType, reqVO.getBusiType())
                .eqIfPresent(PayMqRecordDO::getOrderId, reqVO.getOrderId())
                .eqIfPresent(PayMqRecordDO::getJzbOrderId, reqVO.getJzbOrderId())
                .eqIfPresent(PayMqRecordDO::getDeliverStatus, reqVO.getDeliverStatus())
                .eqIfPresent(PayMqRecordDO::getConsumeStatus, reqVO.getConsumeStatus())
                .orderByDesc(PayMqRecordDO::getId));
    }

    default PayMqRecordDO selectOne(BigInteger orderId, BigInteger jzbOrderId, String busiType, String deliverStatus, String consumeStatus) {
        return this.selectOne((new QueryWrapperX<PayMqRecordDO>()
                .eqIfPresent("order_id", orderId)
                .eqIfPresent("jzb_order_id", jzbOrderId)
                .eq("busi_type", busiType)
                .eqIfPresent("deliver_status", deliverStatus)
                .eqIfPresent("consume_status", consumeStatus)
                .last(" for update ")
        ));
    }

    default PayMqRecordDO selectOne(PayMqRecordDO recordDO) {
        return this.selectOne((new QueryWrapperX<PayMqRecordDO>()
                .eqIfPresent("order_id", recordDO.getOrderId())
                .eqIfPresent("jzb_order_id", recordDO.getJzbOrderId())
                .eq("busi_type", recordDO.getBusiType())
                .eqIfPresent("deliver_status", recordDO.getDeliverStatus())
                .eqIfPresent("consume_status", recordDO.getConsumeStatus())
        ));
    }


    default Integer updateByJzbOrderIdAndBusiType(BigInteger jzbOrderId, String busiType, String deliverStatus, String consumeStatus){
        return update(null,new LambdaUpdateWrapper<PayMqRecordDO>()
                .set(StringUtils.hasLength(deliverStatus), PayMqRecordDO::getDeliverStatus,deliverStatus)
                .set(StringUtils.hasLength(consumeStatus), PayMqRecordDO::getConsumeStatus, consumeStatus)
                .eq(PayMqRecordDO::getJzbOrderId, jzbOrderId)
                .eq(PayMqRecordDO::getBusiType, busiType)
        );
    }


}
